using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;
using Q.Framework.Utility;
using Q.Framework.Extension;

namespace Q.Framework.WebControls
{
    [DefaultProperty("Value")]
    [ToolboxData("<{0}:PeriodControl runat=server></{0}:PeriodControl>")]
    public class PeriodControl : CompositeControl, IBindableControl  
    {
        private DateControl dcInicial = new DateControl();
        private DateControl dcFinal = new DateControl();

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

        }

        public override void RenderControl(HtmlTextWriter writer)
        {
            writer.RenderBeginTag(HtmlTextWriterTag.Table);
            writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding, "0");
            writer.AddAttribute(HtmlTextWriterAttribute.Cellspacing, "0");
            writer.RenderBeginTag(HtmlTextWriterTag.Tr);
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            dcInicial.RenderControl(writer);
            writer.RenderEndTag();
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            writer.AddStyleAttribute(HtmlTextWriterStyle.TextAlign, "center");
            writer.Write("&nbsp;a&nbsp;");
            writer.RenderEndTag();  
            writer.RenderBeginTag(HtmlTextWriterTag.Td);
            dcFinal.RenderControl(writer);
            writer.RenderEndTag();            
            writer.RenderEndTag();
            writer.RenderEndTag();
        }

        public override bool Enabled
        {
            get
            {
                return base.Enabled;
            }
            set { EnsureChildControls(); dcInicial.Enabled = value; dcFinal.Enabled = value; }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public Interval<DateTime> Value
        {
            get
            {
                EnsureChildControls();
                return new Interval<DateTime>(dcInicial.Value, dcFinal.Value);
            }

            set { EnsureChildControls(); if (value != null) { dcInicial.Value = value.Initial; dcFinal.Value = value.Final; } }
        }

        protected override void CreateChildControls()
        {
            
            dcInicial.ID = this.ID + "Data";
            dcFinal.ID = this.ID + "Calendar";
            this.Controls.Add(dcInicial);
            this.Controls.Add(dcFinal);
            base.CreateChildControls();
        }

        public override void Focus()
        {
            EnsureChildControls();
            dcInicial.Focus();
        }

        public Boolean IsEmpty
        {
            get
            {
                EnsureChildControls();
                return dcInicial.Value.IsDefaultValue() || dcFinal.Value.IsDefaultValue();
            }
        }

        public string DataPropertyName { get; set; }

        private String controlBindableProperty = "Value";
        public string ControlBindableProperty
        {
            get { return controlBindableProperty; }
            set { controlBindableProperty = value; }
        }
    }
}